/*
 * AisleNav Android Application
 * ListMenuActivity.java
 * project.AisleNav.ListActivity
 * 
 * CS495
 * 
 * Team:
 * 		Kirby Green
 * 		Mark Hale
 * 		Adam Warnock
 * 
 * Time: 6:14:34 PM
 * Date: Dec 4, 2011
 * 
 */
package project.AisleNav.ListActivity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;

import project.AisleNav.R;
import project.AisleNav.Backend.Item;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

// TODO: Auto-generated Javadoc
/**
 * ListMenuActivity.java Adds, edits, and deletes user lists
 * 
 * @author Kirby Green
 * @version 0.1
 */
public class ListMenuActivity extends Activity {
	
	/** The edit list button. */
	private static Button editListButton;
	
	/** The delete list button. */
	private static Button deleteListButton;
	
	/** The new list button. */
	private static Button newListButton;
	
	/** The new list name text. */
	private static EditText newListNameText;

	/**
	 * Called when the activity is first created.
	 *
	 * @param savedInstanceState the saved instance state
	 * @author Kirby Green
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		System.out.println("At ListMenuActivity");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_edit_list);
		editListButton = (Button) findViewById(R.id.editListButton);
		deleteListButton = (Button) findViewById(R.id.deleteListButton);
		newListButton = (Button) findViewById(R.id.newListButton);
		newListNameText = (EditText) findViewById(R.id.editTextField);

		/**
		 * Should edit a list.
		 */
		editListButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Write editList
			}
		});

		/** Just opens the sample ListView activity ATM. Should delete a list. */
		deleteListButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Write deleteList
				Intent i = new Intent(ListMenuActivity.this, DeleteItemListActivity.class);
				startActivity(i);

			}
		});

		/**
		 * Create a user list. 
		 */
		newListButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Write newList
				Intent i = new Intent(ListMenuActivity.this, CreateNewItemListActivity.class);
				startActivity(i);
				//readCSV();
				//System.out.println("GENERATED LIST");

			}
		});
	}

	/** Read in a CSV to populate the list. */
	// TODO add a filename as a parameter, hard-coded ATM
	public void readCSV() {
		InputStream is;
		LinkedList<Item> itemList = new LinkedList<Item>();
		File dir = Environment.getExternalStorageDirectory();
		try {
			File f = new File(dir, "Item_list.csv");
			is = new FileInputStream(f);
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(is));
			String line;
			try {
				while ((line = reader.readLine()) != null) {
					String[] RowData = line.split(",");
					int id = Integer.parseInt(RowData[0]);
					String name = RowData[1];
					itemList.add(new Item(id, name));
				}
			} catch (IOException e) {
				Toast.makeText(getApplicationContext(), "IOException",
						Toast.LENGTH_SHORT);
				newListNameText.setText("IOException");
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			Toast.makeText(getApplicationContext(), "File not found",
					Toast.LENGTH_SHORT);
			newListNameText.setText("FNF");
			e.printStackTrace();
		}

		//project.AisleNav.AisleNavActivity.SharedLists.add(itemList);
		newListNameText.setText("DONE");
	}

}
